package controllers;

import textfileworker.TextFileWorker;
import model.Garage;
import model.IGarage;
import model.IMaster;
import model.IOrder;
import model.Master;
import model.Order;
import database.IDataBase;
import java.util.GregorianCalendar;

public class FillData {

	public final static String MASTERS_PATH = "e:/java/Masters.txt";
	public final static String GARAGES_PATH = "e:/java/Garages.txt";
	public final static String ORDERS_PATH = "e:/java/Orders.txt";

	public static void fillData(IDataBase db) {

		for (int i = 0; i < 10; i++) {
			IGarage garage = new Garage(i + 1);
			db.addGarage(garage);
		}

		for (int i = 0; i < 10; i++) {
			IMaster master = new Master("Master" + String.valueOf(i + 1));
			db.addMaster(master);
		}

		GregorianCalendar filingDate = new GregorianCalendar(2013, 11, 12);
		for (int i = 0; i < 10; i++) {
			filingDate.add(3, i);
			IOrder order = new Order(i + 1, db.getMasters()[i],filingDate,6000);
			db.addOrder(order);
		}

	}

	public static void fillDataFromFile(IDataBase db) {

		TextFileWorker textFileWorker = new TextFileWorker(GARAGES_PATH);

		String[] garages = textFileWorker.readFromFile();

		for (int i = 0; i < garages.length; i++) {
			IGarage garage = new Garage(Integer.parseInt(garages[i]));
			db.addGarage(garage);
		}

		textFileWorker = new TextFileWorker(MASTERS_PATH);

		String[] masters = textFileWorker.readFromFile();

		for (int i = 0; i < masters.length; i++) {
			IMaster master = new Master(masters[i]);
			db.addMaster(master);
		}

		textFileWorker = new TextFileWorker(ORDERS_PATH);

		String[] orders = textFileWorker.readFromFile();

		GregorianCalendar filingDate = new GregorianCalendar(2013, 11, 12);
		
			for (int i = 0; i < orders.length; i++) {
			filingDate.add(3, i);
			IOrder order = new Order(i + 1, db.getMasters()[i], filingDate,
					6000);
			db.addOrder(order);
		}
		}
	
	public static void writeDbToFile(IDataBase db) {


		IMaster[] masters = db.getMasters();
		IGarage[] garages = db.getGarages();
		IOrder[] orders= db.getOrders();
		
		TextFileWorker textFileWorker = new TextFileWorker(GARAGES_PATH);

		String[] garagesNumbers = new String[garages.length];

		for (int i = 0; i < garages.length; i++) {
			if(garages[i].getNumberGarage() != 0){
				
				garagesNumbers[i] = String.valueOf(garages[i].getNumberGarage());
				
			}
		}
		textFileWorker.writeToFile(garagesNumbers);

		textFileWorker = new TextFileWorker(MASTERS_PATH);

		String[] mastersNames = new String[masters.length];

		for (int i = 0; i < masters.length; i++) {
			if(masters[i].getNameMaster() != null){
			mastersNames[i] = String.valueOf(masters[i].getNameMaster());
			}
		}
		textFileWorker.writeToFile(mastersNames);
		
		textFileWorker = new TextFileWorker(ORDERS_PATH);

		String[] ordersNumbers = new String[orders.length];

		for (int i = 0; i < orders.length; i++) {
			if(orders[i].getNumOrder() != 0){
				
				ordersNumbers[i] = String.valueOf(orders[i].getNumOrder() + "." + orders[i].getPrice() + "." + orders[i].getFilingDate() + "." + orders[i].getPerfomanceDate() + "." + orders[i].getPlannedStartDate() + ".");
			}
		}
		textFileWorker.writeToFile(garagesNumbers);

	}

}
